package com.yonyou.trumpchi;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.google.gson.Gson;
import com.yonyou.trumpchi.entity.ServiceInfo;

public class MainActivity extends AppCompatActivity {
    private WebView mWebView;
    //    private ProgressBar mPageLoadingProgressBar;
    private Context mContext;
    private ServiceInfo serviceInfo;
    public static String HOME_URL="";
    Gson gson = new Gson();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        initData();
        initViews();
    }

    private void initData() {
        String jsonStr = (String) SPUtils.get(mContext, Constants.HOME_URL, "");
        if (!TextUtils.isEmpty(jsonStr)) {
            serviceInfo = gson.fromJson(jsonStr, ServiceInfo.class);
        } else {
            serviceInfo = new ServiceInfo();
            serviceInfo.setUrl(Constants.HOME_URL);
            serviceInfo.setName("广汽传祺");
        }
    }

    /**
     * 初始化webview
     */
    private void initViews() {
        mWebView = (WebView) findViewById(R.id.webView);
//        mPageLoadingProgressBar = (ProgressBar) findViewById(R.id.pageLoadingProgressBar);

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
//        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        /**
         * 用WebView显示图片，可使用这个参数 设置网页布局类型：
         * 1、LayoutAlgorithm.NARROW_COLUMNS ：适应内容大小
         * 2、LayoutAlgorithm.SINGLE_COLUMN:适应屏幕，内容将自动缩放
         */
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.setWebViewClient(new WebViewClient());

        mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);//不加上白边
        mWebView.setWebChromeClient(new WebChromeClient() {

//            @Override
//            public void onProgressChanged(WebView view, int newProgress) {
//                super.onProgressChanged(view, newProgress);
//                mPageLoadingProgressBar.setProgress(newProgress);
//                if (mPageLoadingProgressBar != null && newProgress != 100) {
//                    mPageLoadingProgressBar.setVisibility(View.VISIBLE);
//                } else if (mPageLoadingProgressBar != null) {
//                    mPageLoadingProgressBar.setVisibility(View.GONE);
//                }
//            }
        });
        mWebView.addJavascriptInterface(new JavaScriptInterface(this, serviceInfo, mWebView), "JavaScriptInterface");
        mWebView.loadUrl(Constants.HOME_URL);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
            mWebView.goBack();// 返回前一个页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 连续点击两次退出程序;
     */
    private long exitTimeMillis = System.currentTimeMillis();

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - exitTimeMillis == 0 || System.currentTimeMillis() - exitTimeMillis > 1000) {
            exitTimeMillis = System.currentTimeMillis();
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_LONG).show();
            return;
        } else {
            android.os.Process.killProcess(android.os.Process.myPid());//退出
            System.exit(0);
        }
    }
}
